package com.administration.managedBean;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.http.Cookie;

import com.core.managedBean.BaseBean;

/*
 * @author Karim Sherif
 */
@ManagedBean
@RequestScoped
public class LogoutBean extends BaseBean implements Serializable{
    private static final long serialVersionUID = 1L;
    @PostConstruct
	public void loadData() {
	}

    public String logout() {
        super.getSession().setAttribute("registeredUser",null);
        super.getSession().invalidate();
        
        Cookie[] cookies = super.getRequest().getCookies();
    	if (cookies == null || cookies.length < 1) {
			return null;
		}
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals("userName")) {
				cookies[i].setMaxAge(0);
				cookies[i].setComment("EXPIRING COOKIE at " + System.currentTimeMillis());
				super.getResonse().addCookie(cookies[i]);
				break;
			}

		}
        return "logout";
    }

}